package ce.tedu.boot22.controller;

import ce.tedu.boot22.entity.Emp;
import ce.tedu.boot22.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {

    @Autowired
    EmpMapper empMapper;

    @RequestMapping("/insert")
    public String insert(Emp emp){
        empMapper.insert(emp);
        System.out.println("emp = " + emp);
        return "添加成功<a href='/index.html'>返回首页</a>";
    }
    @RequestMapping("/select")
    public String select(){
        List<Emp> list= empMapper.select();
        String html="<table border=1>";
        html+="<caption></caption>";
        html+="<tr><th>id</th><th>名字</th><th>工资</th><th>工作</th></tr>";
        //遍历集合,并把数据添加到表格中
        for(Emp e : list ){
            html+="<tr>";
            html+="<td>"+e.getId()+"</td>";
            html+="<td>"+e.getName()+"</td>";
            html+="<td>"+e.getSal()+"</td>";
            html+="<td>"+e.getJob()+"</td>";
            html+="<td><a href='/delete?id="+e.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/delete")
    public String delete(int id){
        empMapper.delete(id);
        return "删除成功!<a href='/select'>返回列表页面</a>";
    }
    @RequestMapping("/update")
    public String update(Emp emp){
        System.out.println("emp = " + emp);
        empMapper.update(emp);
        return "修改完成<a href='/select'>返回参数列表</a>";
    }
}
